<?php 
include "../header.inc";

require_once "../DataBase.inc";
require_once($_SESSION["root_path"]."/general_queries.inc");

function percent($nb,$total) {
	if ($total == 0) return "0%";
	return round($nb*100/$total,2)."%";
}

$errors = array();
$queries = array();
function query($q, $w) {
	$fq = $q;
	if (count($w)>0) { 
		$fq .= " WHERE ";
		for ($i = 0; $i < count($w); $i++) {
			if ($i>0) $fq .= " AND ";
			$fq .= $w[$i];
		}
	}
	global $queries;
	array_push($queries, $fq);
	$res = DataBase::query($fq);
	if ($res === false) {
		global $errors;
		array_push($errors, "Error #".DataBase::last_error_number().": ".DataBase::last_error_message()."<br/>Query: <code>".$fq."</code><br/><br/>");
		return 0;
	}
	return $res[0][0];
}

$current_section = null;

function table_section($title, $id) {
	global $current_section;
	$current_section = $id;
	echo "<tr><td colspan=8>";
	echo "<a href='' onclick=\"show_hide_section(this,'".$id."');return false\">";
	echo $title;
	echo "</a>";
	echo "</td></tr>";
}

function table_header($title) {
	global $current_section;
	echo "<tr".($current_section <> null ? " class='".$current_section."' style='visibility:hidden;position:absolute'": "").">";
	echo "<th>".$title."</th><th>Total</th><th colspan=2>Males</th><th colspan=2>Females</th><th colspan=2>Followed by NGO</th>";
	echo "</tr>";
}

function table_row($title,$joins,$where) {
	array_push($where, "applicant.selection_campaign_id=".$_SESSION["campaign_id"]);
	$q = "SELECT COUNT(*) FROM applicant";
	foreach ($joins as $join) $q.= " ".$join;
	
	$total = query($q,$where);
	$males = query($q,array_merge($where, array("applicant.sex='M'")));
	$females = query($q,array_merge($where, array("applicant.sex='F'")));
	$ngos = query($q,array_merge($where, array("applicant.ngo_id IS NOT NULL")));
	
	global $current_section;
	echo "<tr".($current_section <> null ? " class='".$current_section."' style='visibility:hidden;position:absolute'": "").">";
	echo "<td align='left' nowrap='nowrap' class='stats_row_title'>".$title."</td>";
	echo "<td align='center'>".$total."</td>";
	echo "<td align='center'>".$males."</td>";
	echo "<td align='center'>".percent($males,$total)."</td>";
	echo "<td align='center'>".$females."</td>";
	echo "<td align='center'>".percent($females,$total)."</td>";
	echo "<td align='center'>".$ngos."</td>";
	echo "<td align='center'>".percent($ngos,$total)."</td>";
	echo "</tr>";
}

function table($joins, $where) {
	global $current_section, $provinces, $ngos;
	
	echo "<table class='stats'>";
	$current_section = null;
	table_header(""); 
	table_row("<b>Applicants</b>", $joins, $where);
	table_section("By Age","age");
	table_header("Age");
	$today = getdate(time());
	$day = intval($today["mday"]);
	$month = intval($today["mon"]);
	$year = intval($today["year"]);
	$date = ($year-17)."-".$month."-".$day;
	table_row("&lt; 17", $joins, array_merge($where, array("applicant.applicant_date_birth>'".$date."'")));
	for ($age = 17; $age <= 25; $age++) {
		$yesterday = getdate(time()-24*60*60);
		$day = intval($yesterday["mday"]);
		$month = intval($yesterday["mon"]);
		$year = intval($yesterday["year"]);
		$start_date = ($year-$age-1)."-".$month."-".$day;
		$today = getdate(time());
		$day = intval($today["mday"]);
		$month = intval($today["mon"]);
		$year = intval($today["year"]);
		$end_date = ($year-$age)."-".$month."-".$day;
		table_row($age, $joins, array_merge($where, array("applicant.applicant_date_birth>='".$start_date."'","applicant.applicant_date_birth<='".$end_date."'")));
	}
	$yesterday = getdate(time()-24*60*60);
	$day = intval($yesterday["mday"]);
	$month = intval($yesterday["mon"]);
	$year = intval($yesterday["year"]);
	$date = ($year-25-1)."-".$month."-".$day;
	table_row("&gt; 25", $joins, array_merge($where, array("applicant.applicant_date_birth<'".$date."'")));
	table_section("By Province", "province");
	table_header("Province");
	foreach ($provinces as $p) table_row($p["province_name"], $joins, array_merge($where, array("applicant.applicant_origin_province_id=".$p["province_id"])));
	table_section("By NGO", "ngo");
	table_header("NGO");
	foreach ($ngos as $n) table_row($n["ngo_name"], $joins, array_merge($where, array("applicant.ngo_id=".$n["ngo_id"])));
	echo "</table>";
}

$provinces = DataBase::select("province",null,null,"province_name");
$ngos = DataBase::select("ngo",null,null,"ngo_name");
$programs = query_programs()->execute();

?>
<script type='text/javascript'>
function show_hide_section(link,id){
	var table = link.parentNode.parentNode.parentNode;
	for (var i = 0; i < table.childNodes.length; ++i) {
		var tr = table.childNodes[i];
		if (tr.className == id) {
			if (tr.style.visibility == "hidden") {
				tr.style.position = "static";
				tr.style.visibility = "visible";
			} else {
				tr.style.position = "absolute";
				tr.style.visibility = "hidden";
			}
		}
	}
}
</script>
<style type='text/css'>
table.stats {
	empty-cells: show;
	border-color: #505050;
	border-style: solid;
	border-width: 0 0 1px 1px;
	border-spacing: 0px;
}
table.stats th {
	border-color: #505050;
	border-style: solid;
	border-width: 1px 1px 0 0;
	background-color: #C0C0FF;
}
table.stats td {
	border-color: #505050;
	border-style: solid;
	border-width: 1px 1px 0 0;
	background-color: #FFFFFF;
	width: 60px;
}
table.stats td.stats_row_title {
	background-color: #E0E0E0;
}
</style>
